<?php

namespace app\admin\controller\stats;

use app\admin\model\RecoveryOrder;
use app\admin\model\Third;
use app\common\controller\Backend;
use function date;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class User extends Backend
{
    
    /**
     * StatsUser模型对象
     * @var \app\admin\model\StatsUser
     */
    protected $model = null;
    protected $noNeedLogin = ['index'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\StatsUser;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 列表
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        /*昨日的统计数据更新*/
        $startYesTime = strtotime('yesterday');//昨天开始
        $endYesTime = strtotime('yesterday')+86399;//昨天开始
        $yesDetail = $this->model->whereBetween('createtime', [$startYesTime,$endYesTime])->find();

        $beforeYesTotal = Third::where('platform', 'wechat')->where('createtime', '<=', $endYesTime-86400)->count();/*前日总用户量*/
        $yesTotal = Third::where('platform', 'wechat')->where('createtime', '<=', $endYesTime)->count();/*昨日累计的用户*/
        $yesIncrease = $yesTotal - $beforeYesTotal;//昨日新增用户

        $orderYesTotal = RecoveryOrder::whereBetween('createtime', [$startYesTime,$endYesTime])->distinct(true)->field('order_user_id')->count();//计算昨日下单用户数量

//        print_r($orderYesTotal);die;
        if($yesDetail){
            /*存在记录则更新数据*/
            $this->model->where('id', $yesDetail->id)->update(
                [
                    'total'         => $yesTotal,
                    'increase'      => $yesIncrease,
                    'ordercount'    => $orderYesTotal
                ]
            );
        }else{
            /*添加今日统计数据*/
            $this->model->save([
                'total'         => $yesTotal,
                'increase'      => $yesIncrease,
                'ordercount'    => $orderYesTotal,
                'createtime'    => $endYesTime
            ]);
        }
        /*查询今天是否已经添加了数据*/
        $startTime = strtotime(date('Y-m-d').'00:00:00');
        $endTime = $startTime+86399;
        $detail = $this->model->whereBetween('createtime', [$startTime,$endTime])->find();
        $total = Third::where('platform', 'wechat')->where('createtime', '<=', $endTime)->count();/*昨日累计的用户*/
        $increase = $total - $yesTotal;//今日新增用户
        $orderTotal = RecoveryOrder::whereBetween('createtime', [$startTime,$endTime])->distinct(true)->field('order_user_id')->count();//计算昨日下单用户数量
        if($detail){
            /*存在记录则更新数据*/
            $this->model->where('id', $detail->id)->update(
                [
                    'total'         => $total,
                    'increase'      => $increase,
                    'ordercount'    => $orderTotal
                ]
            );
        }else{
            /*添加今日统计数据*/
            $this->model->save([
                'total'         => $total,
                'increase'      => $increase,
                'ordercount'    => $orderTotal,
                'createtime'    => $endTime
            ]);
        }
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order('createtime DESC')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
}
